package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Fm;
import com.hs.service.FmService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("fm")
public class FmController {
    @Autowired
    private FmService fmService;
    @PostMapping
    public R addFm(@RequestBody Fm fm) {
        int i = fmService.addFm(fm);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @DeleteMapping("{id}")
    public R deleteFm(@PathVariable("id") int id){
        int i = fmService.deleteFm(id);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @PutMapping
    public R updateFm(@RequestBody Fm fm){
        int i = fmService.updateFm(fm);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @GetMapping("{id}")
    public R getFmById(@PathVariable("id") int id){
        Fm fm = fmService.getFmById(id);
        return R.ok(fm);
    }
    @GetMapping("page")
    public R getFmByPage(@RequestParam(required = false,defaultValue = "1") int pageNum,
                         @RequestParam(required = false,defaultValue = "3") int pageSize){
        PageInfo<Fm> fmByPage = fmService.getFmByPage(pageNum, pageSize);
        return R.ok(fmByPage);
    }
}
